package com.doradance.app.appactivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.donkingliang.labels.LabelsView;
import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.ShareUtil;
import com.doradance.app.MainActivity;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.AddBabyEntity;
import com.doradance.entity.SelectBabyEntity;
import com.doradance.entity.TagsEntity;
import com.doradance.entity.TextEntity;
import com.doradance.entity.UserHomeEntity;
import com.pachong.android.baseuicomponent.CompState;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.framework.picture.picker.release.SelectAlbumActivity;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.doradance.account.ui.LoginPhoneActivity.LOGINADD;
import static com.doradance.app.mine.MineFragment.MINEADD;
import static com.doradance.app.mine.MineFragment.MINEMODIFY;

public class SelsectLabelActivity extends BaseActivity {

    @Bind(R.id.tv_select_num)
    TextView mTvSelectNum;
    @Bind(R.id.labels)
    LabelsView labelsView;
    private static final int MAXNUM = 8;
    private String nickname;
    private String avatar;
    private String cover;
    private String birthday;
    private String gender;
    private String sign;
    private int userId;
    private int id;
    private int type;
    Button btnStartStudy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolsBar();
        initLabelsView();

        btnStartStudy = (Button) findViewById(R.id.btn_start_study);
        userId = getIntent().getIntExtra("userId", -1);
        id = getIntent().getIntExtra("id", -1);
        type = getIntent().getIntExtra("type", -1);
        nickname = getIntent().getStringExtra("nickname");
        avatar = getIntent().getStringExtra("avatar");
        cover = getIntent().getStringExtra("cover");
        birthday = getIntent().getStringExtra("birthday");
        gender = getIntent().getStringExtra("gender");
        sign = getIntent().getStringExtra("sign");

    }

    private void initLabelsView() {
        MyHttpRequest request = new MyHttpRequest(this);
        request.get(UrlCenter.ALLTAGS, null, new DataRequestListener<List<TagsEntity>>(TagsEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

//            @Override
//            public void enableProgress(boolean enableProgress) {
//                super.enableProgress(enableProgress);
//                if(enableProgress){
//                    enableProgress(false);
//                }
//            }

            @Override
            public void onSuccess(List<TagsEntity> o) {
                super.onSuccess(o);
                hideProgress();
                labelsView.setSelectType(LabelsView.SelectType.MULTI);
                labelsView.setMaxSelect(MAXNUM);
                ArrayList<TextEntity> testList = new ArrayList<>();
                for (int i=0; i<o.size();i++){
                    testList.add(new TextEntity(o.get(i).getName()));
                }
                labelsView.setLabels(testList, new LabelsView.LabelTextProvider<TextEntity>() {
                    @Override
                    public CharSequence getLabelText(TextView label, int position, TextEntity data) {
                        return data.getName();
                    }
                });
                labelsView.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
                    @Override
                    public void onLabelClick(TextView label, Object data, int position) {
                        if (labelsView.getSelectLabels().size() == MAXNUM) {
                            mTvSelectNum.setText(getString(R.string.tv_select_more_interest));
                            EasyToast.showToast(SelsectLabelActivity.this, getString(R.string.tv_select_more_interest));
                        } else {
                            mTvSelectNum.setText(getString(R.string.tv_select_last_interest));
                        }
                    }
                });
                btnStartStudy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startStudy();
                    }
                });
            }
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                hideProgress();
            }
        });
    }

    private void startStudy() {
        if(labelsView.getSelectLabels().size()==0){
            EasyToast.showToast(getApplicationContext(),"请至少选择一个标签");
            return;
        }
        JSONArray selectTags = new JSONArray();
        for(int i=0;i<labelsView.getSelectLabelDatas().size();i++){
            selectTags.put(labelsView.getSelectLabelDatas().get(i).toString());
        }

        MyHttpRequest request = new MyHttpRequest(this);
        JSONObject jobj = new JSONObject();
        if(type ==MINEMODIFY){
            try {
                jobj.put("id",id);
                jobj.put( "user_id",userId);
                jobj.put("nick_name",nickname);
                jobj.put( "avatar",avatar);
                jobj.put("cover",cover);
                jobj.put("birthday",birthday);
                jobj.put("gender",gender);
                jobj.put("sign",sign);
                jobj.put("tags",selectTags);
                Log.e("1111111",nickname+avatar+cover+birthday+gender+sign+selectTags.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            request.put(UrlCenter.ADDBABY, jobj, new DataRequestListener<SelectBabyEntity>(SelectBabyEntity.class) {
                @Override
                public void onSuccess(SelectBabyEntity o) {
                    super.onSuccess(o);
                    getMineInfo();
                }
                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                    EasyToast.showToast(getApplicationContext(),strMsg);
                }
            });
        }else{
            try {
                jobj.put("nick_name",nickname);
                jobj.put( "avatar",avatar);
                jobj.put("cover",cover);
                jobj.put("birthday",birthday);
                jobj.put("gender",gender);
                jobj.put("sign",sign);
                jobj.put("tags",selectTags);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            request.post(UrlCenter.ADDBABY, jobj, new DataRequestListener<SelectBabyEntity>(SelectBabyEntity.class) {
                @Override
                public void onStart(Context context, String url) {
                    super.onStart(context, url);
                }

                @Override
                public void onSuccess(SelectBabyEntity o) {
                    super.onSuccess(o);
                    if(type ==MINEADD){
                        getMineInfo();

                    }else if(type ==LOGINADD){
                        ShareUtil.setBabyId(o.getId()+"");
                        SharePreferenceUtil.saveString(getApplicationContext(),"isOptimize","isOptimize","true");
                        MainActivity.start(SelsectLabelActivity.this);
                    }
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                    EasyToast.showToast(getApplicationContext(),strMsg);
                }
            });
        }
    }

    private void initToolsBar() {
        getCustomToolbar().addLeftImageButton(R.mipmap.back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getCustomToolbar().setTitle(getString(R.string.select_interest));
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_selsect_label, null);
    }

    public static void start(Activity activity,String nickname,String avatar,String cover,String birthday,String gender,String sign) {
        Intent intent = new Intent(activity, SelsectLabelActivity.class);
        intent.putExtra("nickname",nickname);
        intent.putExtra("avatar",avatar);
        intent.putExtra("cover",cover);
        intent.putExtra("birthday",birthday);
        intent.putExtra("gender",gender);
        intent.putExtra("sign",sign);
        activity.startActivity(intent);
    }
    public static void start(Activity activity,int type,String nickname,String avatar,String cover,String birthday,String gender,String sign) {
        Intent intent = new Intent(activity, SelsectLabelActivity.class);
        intent.putExtra("nickname",nickname);
        intent.putExtra("avatar",avatar);
        intent.putExtra("cover",cover);
        intent.putExtra("birthday",birthday);
        intent.putExtra("gender",gender);
        intent.putExtra("sign",sign);
        intent.putExtra("type",type);
        activity.startActivity(intent);
    }

    public static void start(Activity activity,int userId,int id,int type,String nickname,String avatar,String cover,String birthday,String gender,String sign) {
        Intent intent = new Intent(activity, SelsectLabelActivity.class);
        intent.putExtra("userId",userId);
        intent.putExtra("id",id);
        intent.putExtra("nickname",nickname);
        intent.putExtra("avatar",avatar);
        intent.putExtra("cover",cover);
        intent.putExtra("birthday",birthday);
        intent.putExtra("gender",gender);
        intent.putExtra("sign",sign);
        intent.putExtra("type",type);
        activity.startActivity(intent);
    }



    private void getMineInfo() {
        MyHttpRequest request = new MyHttpRequest(SelsectLabelActivity.this);
        request.get(UrlCenter.USERS_HOME, null, new DataRequestListener<UserHomeEntity>(UserHomeEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(UserHomeEntity o) {
                super.onSuccess(o);
                AddBabyActivity.instance.finish();
                finish();
                EventBus.getDefault().post(new AddBabyEntity(o));
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }
}
